<?php
 
// no direct access
defined('_JEXEC') or die('Restricted access');

class SyncrModelSites extends JModel
{
	var $_data	= null;
	
	public function &getData()
	{
		if (empty($this->_data))
		{
			// pagination
			$pagination =& $this->getPagination();
			// query
			$query = $this->_buildQuery();
			// data
			$this->_data = $this->_getList($query, $pagination->limitstart, $pagination->limit);
			
			return $this->_data;
		}
	}
	
	public function &getPagination()
	{
		// pagination library
		jimport('joomla.html.pagination');
		
		$app =& JFactory::getApplication();
		
		// pagination
		$this->_pagination = new JPagination($this->getTotal(), JRequest::getVar('limitstart', 0), JRequest::getVar('limit', $app->getCfg('list_limit')));
		
		$this->_pagination->_viewall = false;
		
		return $this->_pagination;
	}
	
	public function getTotal()
	{
		$query = $this->_buildQuery();
		
		$this->_total = $this->_getListCount($query);
		
		return (int) $this->_total;
	}
	
	private function _buildQuery()
	{
		$db =& JFactory::getDBO();
		
		$this->_query = "SELECT id"
					 .", name"
					 .", url"
					 .", color"
					 .", logo"
					 .", apikey"
					 .", image"
					 .", published"
					 .", checked_out"
					 .", checked_out_time"
					 ."  FROM #__syncr_sites"
					 . $this->_buildQueryWhere()
					 . $this->_buildQueryOrderBy()
					 ;
					 
		return (string) $this->_query;
	}
	
	private function _buildQueryWhere()
	{
		$app	=& JFactory::getApplication();
		$db		=& JFactory::getDBO();
		
		// user state
		$published	= $app->getUserStateFromRequest('com_syncr.filter_state', 'filter_state', '', 'word');
		$search	= $app->getUserStateFromRequest('com_syncr.search', 'search', '', 'string');
		$search = JString::strtolower($search);
		
		// where
		$where = null;
		if ($search)
		{
			$search = '%'.$db->getEscaped($search, true).'%';
			$search = $db->Quote($search, false);
			$places[] = "LOWER(name) LIKE ".$search;
			$places[] = "LOWER(color) LIKE ".$search;
			$places[] = "LOWER(logo) LIKE ".$search;
			
			$search = implode(' OR ', $places);
			$where[] = '('.$search.')';
		}
		
		if ($published) {
			$where[] = 'published = 1';
		}
		
		if (count($where)) {
			$where = ' WHERE '.implode(' AND ', $where);
		}
		
		return (string) $where;
	}
	
	private function _buildQueryOrderBy()
	{
		$app	=& JFactory::getApplication();
		$db		=& JFactory::getDBO();
		
		// user state
		$order	= $app->getUserStateFromRequest('com_syncr.filterOrder', 'filter_order', 'id', 'word');
		if ($order !== 'id'
		 && $order !== 'name'
		 && $order !== 'color'
		 && $order !== 'image'
		 && $order !== 'published'
		 ) {
			$order = 'id';
		}
		
		$orderDirection = $app->getUserStateFromRequest('com_syncr.filterOrderDirection', 'filter_order_Dir', 'ASC', 'cmd');
		$orderDirection = strtoupper($orderDirection);
		if ($orderDirection !== 'ASC'
		 && $orderDirection !== 'DESC'
		 ) {
		 	$orderDirection = 'DESC';
		}
		
		return (string) ' ORDER BY '.$db->nameQuote($order).' '.$orderDirection.' ';
		
	}
}